package Controllers.GenericToolsForController;

import com.adobe.acrobat.Viewer;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import javax.swing.*;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

/**
 * Class to display a PDF document in a JFrame
 */
public class PDFDocument extends JPanel
{
	
  /**
         * 
         */
  private static final long serialVersionUID = 1L;

  public PDFDocument( String filename )
  {
	  openFile(filename);
  }
  
  public PDFDocument( PDFfile file ,String name)
  {
		String currentDir = System.getProperty("user.dir");
		String path = currentDir+"\\Files";
		File dir=new File(path);
		if(! dir.exists())
		{
			dir.mkdir();
			System.out.println("Creats new dir : "+path);
		}

		System.out.println(path);
		
		String FileName;
		
		if(name==null) name = "Temp";
			
		FileName =  path + "\\" + name + ".pdf";
		
		System.out.println(FileName);
		
		File newfile = new File(FileName);
		
		try {
			FileOutputStream fos = new FileOutputStream(newfile);
			fos.write(file.input);
			fos.close();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
		openFile( newfile.getPath().replace("\\" , "/"));
  }

  
/**
 * 
 */
public static void openFile(String filename) {
	try 
	{
		if ((new File(filename)).exists()) 
		{
			Process p = Runtime
			   .getRuntime()
			   .exec("rundll32 url.dll,FileProtocolHandler " + filename);
			p.waitFor();
 
		} 
		else
		{
			System.out.println("File is not exists");
		}
		System.out.println("Done");
	  } 
	catch (Exception ex) 
	  {
		ex.printStackTrace();
	  }
}
  
  



}

